<?php
declare (strict_types=1);

namespace app\admin\model\base;

use app\BaseModel;

/**
 * 用户公众号关注记录表
 */
class UserSubscribeLog extends BaseModel
{

    /**
     * 用户对公众号关注情况记录
     * @param int $user_id
     * @param string $appid
     * @param int $subscribe
     * @param string $openid
     * @param int $thirdId
     * @return bool
     */
    public static function oneLogSave(int $user_id, string $appid,int $subscribe,string $openid,$thirdId=0): bool
    {
        $where = ['openid' => $openid, 'appid' => $appid,'subscribe'=>1];
        $res = self::findOne($where);
        $where['subscribe'] = $subscribe;
        if (!$res && $subscribe==0) {
            $where['subscribe'] = 3;
        }
        //查询日志是否存在
        $where['user_id'] = $user_id;
        if($thirdId){
            $where['third_id'] = $thirdId;
        }
        $where['create_time'] = time();
        $model = new self();
        $model->save($where);
        return true;
    }

}




